package QAInfo::Schema::InfoDB::Reports;

use strict;
use warnings;

use base 'DBIx::Class';

__PACKAGE__->load_components("Core");
__PACKAGE__->table("reports");
__PACKAGE__->add_columns(
  "id",
  { data_type => "INT", default_value => undef, is_nullable => 0, size => 10 },
  "os_id",
  { data_type => "INT", default_value => 0, is_nullable => 1, size => 10 },
  "results_id",
  { data_type => "INT", default_value => 0, is_nullable => 1, size => 10 },
  "users_id",
  { data_type => "INT", default_value => 0, is_nullable => 1, size => 10 },
  "testplans_id",
  { data_type => "INT", default_value => 0, is_nullable => 0, size => 10 },
  "upgradeinfo_id",
  {
    data_type => "VARCHAR",
    default_value => undef,
    is_nullable => 1,
    size => 30,
  },
  "editions_id",
  { data_type => "INT", default_value => 0, is_nullable => 0, size => 10 },
  "computers_name",
  { data_type => "VARCHAR", default_value => 0, is_nullable => 0, size => 25 },
  "languages_id",
  { data_type => "INT", default_value => 0, is_nullable => 0, size => 10 },
  "subject",
  {
    data_type => "VARCHAR",
    default_value => undef,
    is_nullable => 1,
    size => 255,
  },
  "version",
  {
    data_type => "VARCHAR",
    default_value => undef,
    is_nullable => 1,
    size => 20,
  },
  "stat",
  {
    data_type => "VARCHAR",
    default_value => undef,
    is_nullable => 1,
    size => 255,
  },
  "comments",
  {
    data_type => "TEXT",
    default_value => undef,
    is_nullable => 1,
    size => 65_535,
  },
  "links",
  {
    data_type => "TEXT",
    default_value => undef,
    is_nullable => 1,
    size => 65_535,
  },
  "mail_time",
  {
    data_type => "DATETIME",
    default_value => undef,
    is_nullable => 1,
    size => 19,
  },
  "created",
  {
    data_type => "DATETIME",
    default_value => undef,
    is_nullable => 1,
    size => 19,
  },
  "modified",
  {
    data_type => "DATETIME",
    default_value => undef,
    is_nullable => 1,
    size => 19,
  },
  "verdict",
  { data_type => "VARCHAR", default_value => "", is_nullable => 0, size => 255 },
);
__PACKAGE__->set_primary_key("id");


# Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-07-16 00:40:36
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:vUIw6WhSb8lFXjpEd1IOxA

__PACKAGE__->belongs_to(os       => 'QAInfo::Schema::InfoDB::Os',          'os_id');
__PACKAGE__->belongs_to(user     => 'QAInfo::Schema::InfoDB::Users',       'users_id');
__PACKAGE__->belongs_to(testplan => 'QAInfo::Schema::InfoDB::Testplans',   'testplans_id');
__PACKAGE__->belongs_to(upgrade  => 'QAInfo::Schema::InfoDB::Upgradeinfo', 'upgradeinfo_id');
__PACKAGE__->belongs_to(edition  => 'QAInfo::Schema::InfoDB::Editions',    'editions_id');
__PACKAGE__->belongs_to(computer => 'QAInfo::Schema::InfoDB::Computers',   'computers_name');
__PACKAGE__->belongs_to(language => 'QAInfo::Schema::InfoDB::Languages',   'languages_id');

# ============== Methods
sub statistics {
    my $self = shift;
    my $result = get_statistics_data($self->stat);
    return $result;
}

# ============== Utils 
sub get_statistics_data {
    my $text = shift;
    # Final results:
    # Elapsed: 03 day(s), 13:19:13
    # Passed: 550 test(s) (99%)
    # Failed: 4 test(s) (0%)
    return unless defined $text;

    my ( $passed, $failed, $elapsed, $total) = (0, 0, '',0);

    $passed  = $1 if ($text =~ /Passed:\s+?(\d+)\s+test/s);
    $failed  = $1 if ( $text =~ /Failed:\s+?(\d+)\s+test/s );
    $elapsed = $1 if ( $text =~ /Elapsed:\s+?(.+)\n/s );
    $total   = $passed + $failed;

    my $div_total = $total || 1;

    # For future:
    my $pass_percent = $passed * 100 / $div_total;
    my $fail_percent = 100 - $pass_percent;

    # Rounded:
    my $p_percent = int $pass_percent;
    my $f_percent = 100 - $pass_percent; 

    my $result = {
                   elapsed      => $elapsed,
                   passed       => $passed,
                   pass_percent => $p_percent,
                   failed       => $failed,
                   fail_percent => $f_percent,
                   total        => $total
                 };

    return $result;
}

1;
